package com.eastcom.bm.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.eastcom.bm.atom.Atom_DatasetDefine;
import com.eastcom.bm.atom.Atom_DatasetNe;
import com.eastcom.bm.atom.Atom_PublicNe;
import com.eastcom.bm.dao.AbstractDAO;
import com.eastcom.bm.dao.IDataSetNeDao;
import com.eastcom.common.db.JDBCTemplate;
import com.eastcom.common.db.ResultData;

public class DataSetNeDao extends AbstractDAO implements IDataSetNeDao {

	public List<Atom_DatasetNe> getAllDatasetNe() throws Exception {
		List<Atom_DatasetNe> results=new ArrayList<Atom_DatasetNe>();
		
		try{
			String sql="select id,dataset_id,ne_id from qa_dataset_ne";
			ResultData rd=JDBCTemplate.executeSQLQuery(sql, JDBCTemplate.PROPAGATION_NEVER, false, false);
			
			Map<String,Atom_DatasetDefine> datasetDefineCache=new HashMap<String,Atom_DatasetDefine>();
			Map<String,Atom_PublicNe> publicNeCache=new HashMap<String,Atom_PublicNe>();
			
			while(rd.next()){
				Atom_DatasetNe dataSetNe=new Atom_DatasetNe();
				dataSetNe.setId(rd.getString("ID"));
				
				Atom_DatasetDefine datasetDefine=datasetDefineCache.get(rd.getString("DATASET_ID"));
				if(datasetDefine==null){
					datasetDefine=new Atom_DatasetDefine();
					datasetDefine.setId(rd.getString("DATASET_ID"));
					datasetDefineCache.put(datasetDefine.getId(), datasetDefine);
				}
				dataSetNe.setDatasetDefine(datasetDefine);
				
				Atom_PublicNe publicNe=publicNeCache.get(rd.getString("NE_ID"));
				if(publicNe==null){
					publicNe=new Atom_PublicNe();
					publicNe.setId(rd.getString("NE_ID"));
					publicNeCache.put(publicNe.getId(), publicNe);
				}
				dataSetNe.setPublicNe(publicNe);
				
				results.add(dataSetNe);
			}

			return results;
		}
		catch(Exception e){
			throw e;
		}
	}

}
